package com.afsadmedia.pregnancy;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class Registeration extends Activity{
	
	
	EditText ET_name, ET_email, ET_dob;
	
	Spinner reg_spinner;
	Button register;
	
	String name, email,dob, gender;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		
		ET_name = (EditText)findViewById(R.id.et_registername);
		ET_email = (EditText)findViewById(R.id.et_registeremail);
		ET_dob = (EditText)findViewById(R.id.et_registerdob);
		reg_spinner =(Spinner)findViewById(R.id.spinner_register);
		register = (Button)findViewById(R.id.btn_registerid);
		
		
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.gender_array,
						android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		reg_spinner.setAdapter(adapter);
		reg_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
		
		register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				
				if (ET_name.getText().toString().equals("")) {
				
					Toast.makeText(getApplicationContext(), "Please enter your Name", Toast.LENGTH_LONG).show();
				} else if (ET_email.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(), "Please enter your Email Id", Toast.LENGTH_LONG).show();
					
				}else if (!ET_name.getText().toString().equals("")
						&& !ET_email.getText().toString().equals("")&&!ET_dob.getText().toString().equals("")
						&& eMailValidation(ET_email.getText().toString()) != true)
						{
					
					Toast.makeText(getApplicationContext(), "Please enter Correct Email Id", Toast.LENGTH_LONG).show();
				}  else if (ET_dob.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(), "Please enter your Date of Birth", Toast.LENGTH_LONG).show();
	
				} else {
			
					try {
						register();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				

				
				
				
				
			}
		});
		
		
		
	}
	
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
				long arg3) {
			// TODO Auto-generated method stub
			 Constants.selectgender = parent.getItemAtPosition(pos).toString();
			 Log.i("UserLevelData",Constants.selectgender);

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}
	
	
	public boolean eMailValidation(String emailstring) {
		Pattern emailPattern = Pattern
				.compile("^[\\w\\-]+(\\.[\\w\\-]+)*@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$");
		Matcher emailMatcher = emailPattern.matcher(emailstring);
		return emailMatcher.matches();
	}

	
	
	public void register() throws Exception {
		ArrayList<String> res_cat = new ArrayList<String>();
		try {
			InputStream is = null;

			String result = "";
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("name",ET_name.getText().toString()));
			nameValuePairs.add(new BasicNameValuePair("mail",ET_email.getText().toString()));
			nameValuePairs.add(new BasicNameValuePair("gender",Constants.selectgender));
			nameValuePairs.add(new BasicNameValuePair("dob",ET_dob.getText().toString()));
			HttpClient httpclient = new DefaultHttpClient();
     		HttpPost httppost = new HttpPost(
					"http://www.nightskincream.com/pregnancy/register.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			
		} catch (Exception e) {
			Toast.makeText(Registeration.this, "Error in Server", Toast.LENGTH_LONG)
					.show();
			Log.v("in server ", e.getMessage());
		}
		
	}
}
